package com.tanhua.server.controller;

import com.tanhua.server.service.SoundService;
import com.tanhua.server.vo.SoundVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/peachblossom")
@Slf4j
public class SoundController {

    @Autowired
    private SoundService soundService;

    //桃花传音:发送
    @PostMapping
    public ResponseEntity<Void> saveSound(@RequestParam("soundFile") MultipartFile file) {

        try {
            String s = this.soundService.saveSound(file);
            if (null != s) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error(e + "");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //桃花传音:接收
    @GetMapping
    public ResponseEntity<SoundVo> receiveSound() {

        try {
            SoundVo soundVo = this.soundService.receiveSound();
            if (null != soundVo) {
                return ResponseEntity.ok(soundVo);
            }
        } catch (Exception e) {
            log.error(e + "");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}
